//
//  SJGuideView.m
//  WSL
//
//  Created by CJ on 2018/2/7.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJGuideView.h"

@interface SJGuideView ()
@property (nonatomic, copy) NSString *imageName;

@property (nonatomic, strong) UIWindow *window;
@end

@implementation SJGuideView

+ (instancetype)guideViewWithImageName:(NSString *)imageName {
    return [[self alloc] initWithImageName:imageName];
}

- (instancetype)initWithImageName:(NSString*)imageName{
    if (self = [super init]) {
        _imageName = imageName;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = CGRectMake(0, 0, SJScreenW, SJScreenH);
    
    UIImage *image = [UIImage imageNamed:_imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, SJScreenW, SJScreenH);
    imageView.userInteractionEnabled = YES;
    imageView.tag = 1000;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchImageView:)];
    [imageView addGestureRecognizer:tap];
    [self addSubview:imageView];
    
    [self show];
}

- (void)touchImageView:(UITapGestureRecognizer*)tap {
    UIImageView *tapImageView = (UIImageView*)tap.view;
    //依次移除
    [tapImageView removeFromSuperview];
    if (tapImageView.tag - 1000 == 0) {
        //最后一张
        if (self.lastTapBlock) {
            self.lastTapBlock();
        }
        [self hide];
    }
}

- (void)show {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.windowLevel = MAXFLOAT;
    [self.window addSubview:self];
    [self.window makeKeyAndVisible];
    
}

- (void)hide {
    
    [self removeFromSuperview];
    self.window.hidden = YES;
    self.window = nil;
}

+ (BOOL)shouldShowGuider:(NSString *)name {
    
    NSString *key = [NSString stringWithFormat:@"guide_%@", name];
    
    NSNumber *number = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    if ([number isEqual:@200]) {
        return NO;
    } else {
        [[NSUserDefaults standardUserDefaults]setObject:@200 forKey:key];
        [[NSUserDefaults standardUserDefaults]synchronize];
        return YES;
    }
}

@end

